// 召唤兽赐福  {"锁定":[],"认证码":"24_1753461117068","bb":1,"材料":18}
// @ts-ignore
export async function execute(client_id, message_json) {
    let characters = G.Characters.get_current_login_characters(client_id);

    let pet_index = message_json["bb"] - 1;
    let pet_data = characters["召唤兽"][pet_index];
    if (pet_data === undefined) {
        ClientUtils.send_basic_prompt(client_id, "召唤兽不存在");
        return;
    }
    if (pet_data["技能"].length < 4) {
        ClientUtils.send_basic_prompt(client_id, "召唤兽技能需要至少四个以上才能赐福");
        return;
    }

    let item_index = message_json["材料"] - 1;
    let item = characters["道具"][item_index];
    if (!item) {
        ClientUtils.send_basic_prompt(client_id, "材料不存在");
        return;
    }
    if (item["名称"] !== "仙露丸子") {
        ClientUtils.send_basic_prompt(client_id, "材料错误");
        return;
    }
    let lock_skill = message_json["锁定"];
    if (lock_skill.length === undefined) {
        lock_skill = [];
    }
    // 处理一下锁定技能数据，因为客户端会把空的数据发过来
    lock_skill = lock_skill.filter(item => item !== null);
    
    if (lock_skill.length > 2) {
        ClientUtils.send_basic_prompt(client_id, "最多只可以锁定两个技能");
        return;
    }
    // 校验客户端的锁定技能数据是否非法
    if (pet_data["赐福"] || pet_data["丸子技能"]) {
        let tmp_skills = [];
        if (pet_data["丸子技能"]) {
            tmp_skills = pet_data["丸子技能"].map(v => v["名称"])
        }
        for (let index = 0; index < lock_skill.length; index++) {
            let lock_name = lock_skill[index];
            if (!pet_data["赐福"].includes(lock_name) && !tmp_skills.includes(lock_name)) {
                ClientUtils.send_basic_prompt(client_id, "#R非法数据");
                return;
            }
        }
    } else if (lock_skill.length > 0) {
        ClientUtils.send_basic_prompt(client_id, "#R非法数据");
        return;
    }

    let consume_num_data = [1, 5, 25];
    let consume_item_num = consume_num_data[Object.keys(message_json["锁定"]).length];
    if (item["数量"] < consume_item_num) {
        ClientUtils.send_basic_prompt(client_id, `仙露丸子不足，本次赐福需要#R${consume_item_num}#Y/个仙露丸子`);
        return;
    }
    // 扣除材料
    G.Characters.remove_item(characters["id"], "道具", item_index, consume_item_num);

    let pet_cf_skills = JSON.parse(JSON.stringify(G.Pet.pet_cf_skills));

    // 排除已锁定技能
    for (let index = 0; index < lock_skill.length; index++) {
        let element = lock_skill[index];
        let lock_index = pet_cf_skills.indexOf(element);
        pet_cf_skills.splice(lock_index, 1);
    }
    let cf_skill_list = JSON.parse(JSON.stringify(lock_skill));
    // 随机取四个技能
    for (let index = 0; index < 4; index++) {
        if (!cf_skill_list[index]) {
            let res = G.RandomUtils.random_get_array_return_index(pet_cf_skills);
            cf_skill_list.push(res[1]);
            pet_cf_skills.splice(res[0], 1);
        }
    }
    pet_data["赐福"] = cf_skill_list;
    // 概率获得超级魔兽要诀
    if (G.RandomUtils.custom_chance(300, consume_item_num)) {
        G.Characters.add_item(characters["id"], "超级魔兽要诀", 1);
    }
    // 概率获得仙露小丸子
    if (G.RandomUtils.chance(consume_item_num)) {
        G.Characters.add_item(characters["id"], "仙露小丸子", G.RandomUtils.generate_random(8, 12));
    }
    // 概率获得神兽涎
    if (G.RandomUtils.chance(consume_item_num)) {
        G.Characters.add_item(characters["id"], "神兽涎", 1);
    }
    // 刷新道具
    G.Characters.refrush_item_data(characters["id"], ["道具"]);
    // 刷新赐福窗口数据
    let send_item_data = {
        "道具": {
            "银子": characters["银子"],
            "储备": characters["储备"],
            "存银": characters["存银"],
            "道具": characters["道具"],
        },
        "丸子赐福": {
            "进程": 3,
            "技能": cf_skill_list,
            "认证码": pet_data["认证码"],
        }
    };
    ClientUtils.send_client_message(client_id, 353, send_item_data);
}